<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报修部门" prop="departmentName">
        <el-input v-model="queryParams.departmentName" placeholder="请输入报修部门" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="报修人" prop="reporterName">
        <el-input v-model="queryParams.reporterName" placeholder="请输入报修人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['day:repair:add']">申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['day:repair:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['day:repair:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['day:repair:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报修部门" align="center" prop="departmentName" />
      <el-table-column label="门牌号" align="center" prop="doorNumber" />
      <el-table-column label="报修人" align="center" prop="reporterName" />
      <el-table-column label="报修内容" align="center" prop="repairContent">
        <template #default="scope">
          <dict-tag :options="repair_content"
            :value="scope.row.repairContent ? scope.row.repairContent.split(',') : []" />
        </template>
      </el-table-column>
      <el-table-column label="报修日期" align="center" prop="repairDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.repairDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修时间" align="center" prop="repairTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修人" align="center" prop="repairPerson" />
      <el-table-column label="维修状态" align="center" prop="repairResult">
        <template #default="scope">
          <dict-tag :options="repair_status" :value="scope.row.repairResult" />
        </template>
      </el-table-column>
      <el-table-column label="维修说明" align="center" prop="repairDescription" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['day:repair:edit']">维修</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['day:repair:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
    <!-- 维修对话框 -->
    <el-dialog :title="title" v-model="open2" width="500px" append-to-body>
      <el-form ref="repairRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修部门" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入报修部门" />
        </el-form-item>
        <el-form-item label="门牌号" prop="doorNumber">
          <el-input v-model="form.doorNumber" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="报修人" prop="reporterName">
          <el-input v-model="form.reporterName" placeholder="请输入报修人" />
        </el-form-item>
        <el-form-item label="报修内容" prop="repairContent">
          <el-checkbox-group v-model="form.repairContent">
            <el-checkbox v-for="dict in repair_content" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="报修日期" prop="repairDate">
          <el-date-picker clearable v-model="form.repairDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择报修日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修说明" prop="repairDescription">
          <el-input v-model="form.repairDescription" placeholder="请输入维修说明，长度限制为50个字符" />
        </el-form-item>
        <el-form-item label="维修结果" prop="repairResult">
          <el-select v-model="form.repairResult" placeholder="请选择维修状态">
            <el-option label="已解决" value="1"></el-option>
            <el-option label="未解决" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改维修对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="repairRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门牌号" prop="doorNumber">
          <el-input v-model="form.doorNumber" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="报修内容" prop="repairContent">
          <el-checkbox-group v-model="form.repairContent">
            <el-checkbox v-for="dict in repair_content" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="报修日期" prop="repairDate">
          <el-date-picker clearable v-model="form.repairDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择报修日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修说明" prop="repairDescription">
          <el-input v-model="form.repairDescription" placeholder="请输入维修说明，长度限制为50个字符" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Repair">
import { listRepair, getRepair, delRepair, addRepair, updateRepair } from "@/api/day/repair";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
const { repair_content } = proxy.useDict('repair_content');
const { repair_status } = proxy.useDict('repair_status');
const open2 = ref(false);
const repairList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    departmentName: null,
    doorNumber: null,
    reporterName: null,
    repairContent: null,
    repairDate: null,
    repairTime: null,
    repairPerson: null,
    repairResult: null,
    repairDescription: null
  },
  rules: {
    departmentName: [
      { required: true, message: "报修部门不能为空", trigger: "blur" }
    ],
    reporterName: [
      { required: true, message: "报修人不能为空", trigger: "blur" }
    ],
    repairContent: [
      { required: true, message: "报修内容不能为空", trigger: "blur" }
    ],
    repairDate: [
      { required: true, message: "报修日期不能为空", trigger: "blur" }
    ],
    repairResult: [
      { required: true, message: "维修状态不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询维修列表 */
function getList() {
  loading.value = true;
  listRepair(queryParams.value).then(response => {
    repairList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  open2.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    repairId: null,
    departmentName: null,
    doorNumber: null,
    reporterName: null,
    repairContent: [],
    repairDate: null,
    repairTime: null,
    repairPerson: null,
    repairResult: null,
    repairDescription: null
  };
  proxy.resetForm("repairRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.repairId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加维修";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _repairId = row.repairId || ids.value
  getRepair(_repairId).then(response => {
    form.value = response.data;
    form.value.repairContent = form.value.repairContent.split(",");
    open2.value = true;
    title.value = "修改维修";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["repairRef"].validate(valid => {
    if (valid) {
      form.value.repairContent = form.value.repairContent.join(",");
      if (form.value.repairId != null) {
        updateRepair(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open2.value = false;
          getList();
        });
      } else {
        addRepair(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _repairIds = row.repairId || ids.value;
  proxy.$modal.confirm('是否确认删除维修编号为"' + _repairIds + '"的数据项？').then(function () {
    return delRepair(_repairIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('day/repair/export', {
    ...queryParams.value
  }, `repair_${new Date().getTime()}.xlsx`)
}

getList();
</script>
